<template>
    <div>
        <m-table :columns="columns" ref="tableRef" :title="$route.meta.title"  :api="getOrganizationList" :pagination="false">
            <template #button>
                <a-button type="primary" size="small" @click="handleEdit('add')" v-perms="['/adminapi/organize.organization/save']">
                    <icon name="ant-icon-PlusOutlined" />
                </a-button>
            </template>


            <template #action="{ record }">
                <a-button type="link" size="small" @click="handleEdit('edit', record)" v-perms="['/adminapi/organize.organization/update']">编辑</a-button>
                <a-popconfirm title="是否确定删除此组织?" @confirm="handleDelete(record.id)">
                    <a-button type="link" size="small" danger v-perms="['/adminapi/organize.organization/delete']">删除</a-button>
                </a-popconfirm>
            </template>

            <template #leader_id="{ record }">
                <a-tag v-if="record.leader_id" color="blue">{{ record.leader_id_name }}</a-tag>
                <a-tag v-else color="red">暂无</a-tag>
            </template>

            <template #status="{ record }">
                <a-tag v-if="record.status" color="green">正常</a-tag>
                <a-tag v-else color="red">禁用</a-tag>
            </template>
        </m-table>
        <edit-popup ref="editPopupRef" @success="() => tableRef?.run()" />
    </div>
</template>
<script setup lang="ts">
import { deleteOrganization } from '@/api/system/organize/organization'
import editPopup from './components/edit.vue'
const editPopupRef = ref<InstanceType<typeof editPopup>>()
const tableRef = ref()
import { getOrganizationList } from '@/api/system/organize/organization'
const columns = [
    {
        title: 'ID',
        dataIndex: 'id',
        width: 100,
        align: 'center',
    },
    {
        title: '组织名称',
        dataIndex: 'title',
    },{
        title:'创建者',
        dataIndex:'creator_id_name',
        width:200,
    },{
        title:'负责人',
        dataIndex:'leader_id',
        width:200,
    },{
        title:'状态',
        dataIndex:'status',
        width:100,
        align:'center',
    },{
        title:'创建时间',
        dataIndex:'create_time',
        width:240,
    },{
        title:"操作",
        dataIndex:"action",

    }

]


const handleEdit = (mode: 'add' | 'edit', record?: any) => {
    editPopupRef.value?.open(mode, record)
}

const handleDelete =  async (id: any) => {
    await deleteOrganization(id)
    tableRef.value?.run()
}
</script>
